/*
 * Same of SemanticListView
 */

package android.bluebox.view;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluebox.R;
import android.bluebox.model.Attribute;
import android.bluebox.model.AttributeAdapter;
import android.bluebox.model.StaticBox;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AttributeListView extends Activity {

	ListView lvAttribute;
	ArrayList<Attribute> attributeList;
	AttributeAdapter cba;

	TextView txtNumberOfAttributes;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attributelist);
		
		StaticBox.dbBox.setContext(this);
		txtNumberOfAttributes = (TextView) findViewById(R.id.al_number);
		
		lvAttribute = (ListView) findViewById(R.id.al_list);
		cba = new AttributeAdapter(this, attributeList);
		refreshAttributeList();
		lvAttribute.setOnItemClickListener(chooseAttribute);
	}

	public ArrayList<Attribute> loadAttribute() {
		String strNumberOfAttributes = String.valueOf(StaticBox.dbBox.getNumberOfAttributes());
		txtNumberOfAttributes.setText(strNumberOfAttributes);
		ArrayList<Attribute> list = StaticBox.dbBox.getAllAttributes();
		return list;
	}
	
	public void refreshAttributeList() {
		attributeList = loadAttribute();
		cba.setArraylist(attributeList);
		cba.notifyDataSetChanged();
		lvAttribute.setAdapter(cba);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.attributelist_optionmenu, menu);
		return true;
	}

	/*
	 * Create event for Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent;

		switch (item.getItemId()) {
		case R.id.al_new_attr:

			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("New Attribute");
			alert.setMessage("Name");

			// Set an EditText view to get user Input
			final EditText edtName = new EditText(this);
			alert.setView(edtName);

			alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {
					String newName = edtName.getText().toString().trim(); 
					if (newName.length() > 0) {
						createNewAttribute(newName);
					} else {
						Toast.makeText(getBaseContext(), "Please input attribute name", Toast.LENGTH_SHORT).show();
					}
				}
			});

			alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {
					// TODO Auto-generated method stub
				}
			});
			alert.show();
			break;

		case R.id.al_change_ws:
			intent = new Intent(AttributeListView.this, WorkspaceListView.class);
			startActivity(intent);
			break;
			
		case R.id.al_change_tag:
			intent = new Intent(AttributeListView.this, TagListView.class);
			startActivity(intent);
			break;
		}
		return true;
	}

	public void createNewAttribute(String newName) {
		if (StaticBox.dbBox.getAttribute(newName) == null) {
			Attribute attribute = new Attribute();
			attribute.setName(newName);
			StaticBox.dbBox.insertAttribute(attribute);
			refreshAttributeList();
		}
	}

	OnItemClickListener chooseAttribute = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> a, View view, int position, long id) {
			final Attribute attribute = (Attribute) lvAttribute.getItemAtPosition(position);

			/*
			 * Create list of option: Connect, Edit, Delete
			 */
			final CharSequence[] attributeOption = {"Open", "Edit", "Delete"};

			/*
			 * Create dialog
			 */
			AlertDialog.Builder builder = new AlertDialog.Builder(AttributeListView.this);
			builder.setTitle(attribute.getName() + " Options");
			builder.setItems(attributeOption, new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int item) {
					/*
					 * Open
					 */
					switch(item) {
					case 0:
						Intent intent = new Intent(AttributeListView.this, AValueListView.class);
						intent.putExtra("typeId", 0);
						intent.putExtra("filterId", attribute.getId());
						startActivity(intent);
						break;

						/*
						 * Edit
						 */
					case 1:
						editAttributeName(attribute.getName());
						break;

						/*
						 * Delete
						 */
					case 2:
						deleleAttribute(attribute);
						break;
					}

				}

			});
			AlertDialog dialog = builder.create();

			/*
			 * Show dialog
			 */
			dialog.show();
		}
	};

	public void changeAttributeName(String oldName, String newName) {
		Attribute attribute = StaticBox.dbBox.getAttribute(oldName);
		if (attribute != null) {
			attribute.setName(newName);
			StaticBox.dbBox.updateAttribute(attribute);
		}
		refreshAttributeList();
	}

	public void deleleAttribute(Attribute attribute) {
		StaticBox.dbBox.deleteAttribute(attribute.getId());
		refreshAttributeList();
	}

	public void editAttributeName(final String oldName) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Edit Attribute");
		alert.setMessage("Attribute Name");

		// Set an EditText view to get user Input
		final EditText edtName = new EditText(this);
		edtName.setText(oldName);
		alert.setView(edtName);

		alert.setPositiveButton("Change", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				String newName = edtName.getText().toString().trim(); 
				if (newName.length() > 0) {
					changeAttributeName(oldName, newName);
					refreshAttributeList();
				} else {
					Toast.makeText(getBaseContext(), "Fail create attribute", Toast.LENGTH_SHORT).show();
				}
			}
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
			}
		});
		alert.show();
	}
}
